﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using RayD.Core;
using RayD.Repository.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.Magicodes;
using RayD.App.SystemModule;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 用户资源
    /// </summary>
    public class UserController : BaseAdminController
    {
        #region 构造函数注入
        private UserApp _User;
        public UserController(UserApp User)
        {
            this._User = User;
        }
        #endregion

        #region 参数实体
        /// <summary>
        /// 分页数据查询响应参数
        /// </summary>
        public class GetPageRespone
        {
            /// <summary>
            /// 查询响应数据
            /// </summary>
            public IEnumerable<UserEntity> rows { get; set; }
            /// <summary>
            /// 总记录数
            /// </summary>
            public int records { get; set; }
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetPageRespone> GetPageList(string queryjson, string pagination)
        {
            var paginationObj = pagination.ToObject<Pagination>();
            var data = _User.GetPageList(queryjson, paginationObj);
            return Success(new GetPageRespone()
            {
                rows = data,
                records = paginationObj.records
            });
        }
        /// <summary>
        /// 根据架构Id及部门Id获取用户列表数据
        /// </summary>
        /// <param name="B_ManaframeId">组织架构Id</param>
        /// <param name="B_DepartmentId">部门Id</param>
        /// <param name="isDeep">是否深度查询</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<UserEntity>> GetListByManaDepId(string B_ManaframeId, string B_DepartmentId, bool isDeep)
        {
            var list = _User.GetListByManaDepId(B_ManaframeId, B_DepartmentId, isDeep);
            return Success(list);
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体(新增,编辑)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut, Logging("用户数据保存")]
        public ResParameter SaveForm(UserEntity request)
        {
            var (isSuccess, msg) = _User.SaveForm(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut, Logging("用户数据删除")]
        public ResParameter DeleteForm(UserEntity request)
        {
            var (isSuccess, msg) = _User.DeleteForm(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 状态更改
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns></returns>
        [HttpPut, Logging("用户数据状态更改")]
        public ResParameter ChangeState(UserEntity request)
        {
            var (isSuccess, msg) = _User.ChangeState(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }

        #endregion

        #region 数据验证
        /// <summary>
        /// 验证字段值是否存在
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <param name="field">字段名</param>
        /// <param name="value">字段值</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter ValidateExist(string id, string field, string value)
        {
            var res = _User.ValidateExist(id, field, value);
            if (!res)
            {
                return Success("校验通过");
            }
            else
            {
                return Fail("值已存在，校验不通过！");
            }
        }
        #endregion

        #region 扩展操作
        /// <summary>
        /// 列表数据导出
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Export(string queryjson)
        {
            //获取数据源
            var data = _User.GetList(queryjson).Where(c => !c.B_SecurityLevel.Equals(1));
            List<ExportUserDto> list = data.MapToList<ExportUserDto>();
            //文件流导出
            var bytes = MagicodeReport.ExportExcel("用户信息", list);
            return File(bytes, new FileExtensionContentTypeProvider().Mappings[".xlsx"]);
        }

        /// <summary>
        /// 下载导出模板
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadTemplate()
        {
            //文件流导出
            var bytes = MagicodeReport.ExportExcelTempl<ImportUserDto>("用户导入模板");
            return File(bytes, new FileExtensionContentTypeProvider().Mappings[".xlsx"]);
        }
        /// <summary>
        /// 导入用户数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResParameter<string> Import()
        {
            //保存到硬盘
            var fileInfo = IOHelper.Save(AppSettings.UploadPath.fileExcel);
            //没有文件上传，直接返回
            if (fileInfo.IsEmpty())
            {
                return Fail("没有文件上传!", "");
            }
            //验证导入的Excel文件
            if (MagicodeReport.ValidImportExcel<ImportUserDto>(fileInfo.FullPath, out var sources, out string msg, out string errorPath))
            {
                var list = sources.MapToList<UserEntity>();
                _User.Import(list);
                return Success(msg, errorPath);
            }
            else
            {
                return Fail(msg, errorPath);
            }
        }
        #endregion
    }
}
